package com.sunday.haowushop.ui.mine.details;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.haowushop.R;
import com.sunday.haowushop.adapter.CommonAdapter;
import com.sunday.haowushop.adapter.ViewHolder;
import com.sunday.haowushop.common.BaseApp;

import com.sunday.haowushop.entity.ShareDetail;
import com.sunday.haowushop.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/6/9.
 * 邀请明细
 */

public class ShareDetailActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.title_layout)
    LinearLayout titleLayout;

    private CommonAdapter<ShareDetail> adapter;
    private List<ShareDetail> dataSet = new ArrayList<>();
    private long memberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_detail);
        ButterKnife.bind(this);
        titleView.setText("分享记录");
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        memberId = BaseApp.getInstance().getMember().getId();
        initRecyclerView();
        getData();
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonAdapter<ShareDetail>(mContext, R.layout.item_share_detail, dataSet) {
            @Override
            public void convert(ViewHolder holder, ShareDetail shareDetail) {
                bind(holder, shareDetail);
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());
    }

    private void bind(ViewHolder holder, ShareDetail shareDetail) {
        ((TextView)holder.getView(R.id.mobile)).setText(shareDetail.getMobile());
        ((TextView)holder.getView(R.id.name)).setText(shareDetail.getNickName());
        ((TextView)holder.getView(R.id.status)).setText(shareDetail.getStatus()==0?"未支付":"已支付");
        ((TextView)holder.getView(R.id.status)).setTextColor(shareDetail.getStatus()==0?
                mContext.getResources().getColor(R.color.colorPrimary): mContext.getResources().getColor(R.color.black_3));
        ((TextView)holder.getView(R.id.date)).setText(shareDetail.getCreateTime());

    }

    private void getData() {
        Call<ResultDO<List<ShareDetail>>> call = ApiClient.getApiAdapter().getShareList(memberId);
        call.enqueue(new Callback<ResultDO<List<ShareDetail>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<ShareDetail>>> call, Response<ResultDO<List<ShareDetail>>> response) {
                if (isFinish) {
                    return;
                }
                ResultDO<List<ShareDetail>> resultDO = response.body();
                if (resultDO == null) {
                    return;
                }

                if (resultDO.getCode() == 0) {
                    if (resultDO.getResult() == null) {
                        return;
                    }
                    dataSet.clear();
                    dataSet.addAll(resultDO.getResult());
                    if (dataSet.size() > 0) {
                        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    } else {
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                    }
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<ShareDetail>>> call, Throwable t) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        });
    }


}
